package com.magicode.cms.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.magicode.cms.dao.INewsCollectDao;
import com.magicode.cms.entiy.base.News;
import com.magicode.cms.entiy.base.NewsCollect;
import com.magicode.common.dao.PageResults;
@Repository("newsCollectDao")
public class NewsCollectDaoImpl extends BaseDaoImpl<NewsCollect> implements INewsCollectDao{

	@Override
	public PageResults Newslist(Map<String, String> prams, Integer pageNo,
			Integer pageSize) {
		String hql = "select n from "+NewsCollect.class.getName()+" t , "+News.class.getName()+" n  left outer join fetch n.mainPic " +
				"  where t.news = n and  t.author.id = '"+prams.get("userid")+"'" +" order by t.createTime desc" ;
		
		String counthql= "select count(t.id) from "+NewsCollect.class.getName()+" t " +
				"  where  t.author.id = '"+prams.get("userid")+"'" ;
		PageResults pageResults = commonDaoService.findPageByFetchedHql(hql, null, pageNo==null?1:pageNo, pageSize, new String[]{});
		return pageResults;
		
	}

	@Override
	public boolean deleteNewsCollect(String userid, String newid) {
		try {
			List<NewsCollect> list = this.getNewsCollects(userid, newid);
			for(NewsCollect nc : list){
				commonDaoService.delete(nc);
			}
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
		
		
	}
	public List<NewsCollect> getNewsCollects(String userid, String newid) throws Exception{
		String hql =" from "+NewsCollect.class.getName()+" t where t.news.id=:newid  and t.author.id=:userid";
		Map<String,String> map = new HashMap<String,String>();
		map.put("newid", newid);
		map.put("userid", userid);
		List<NewsCollect> list = commonDaoService.queryList(hql, map);
		return list;
	}
	
	
}
